package star.cloud.asme.consumable.overview;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import star.cloud.asme.base.dto.Result;
import star.cloud.asme.base.mybatis.IBaseServiceImpl;

import java.util.List;

@Service
@Slf4j
@RequiredArgsConstructor
public class ConsumableServiceImpl extends IBaseServiceImpl<ConsumableMapper, Consumable> implements ConsumableService {

    private final ConsumableMapper consumableMapper;

    @Override
    public Result<Consumable> getOneByCondition(String columnName, Object columnValue) {
        LambdaQueryWrapper<Consumable> queryWrapper = new LambdaQueryWrapper<>();

        if (columnName.equals("id")) {
            queryWrapper.eq(Consumable::getId, columnValue);
        }

        Consumable consumable = consumableMapper.selectOne(queryWrapper);

        setCreatorAndUpdater(consumable);

        return Result.ok(consumable);
    }

    @Override
    public Result<List<Consumable>> getListByCondition(String columnName, Object columnValue) {
        LambdaQueryWrapper<Consumable> queryWrapper = new LambdaQueryWrapper<>();
        List<Consumable> asmePersonInfoList = consumableMapper.selectList(queryWrapper);
        return Result.ok(asmePersonInfoList);
    }

    @Override
    public Result<List<Consumable>> getListByConditions(Consumable conditions) {
        LambdaQueryWrapper<Consumable> queryWrapper = new LambdaQueryWrapper<>();
        List<Consumable> asmePersonInfoList = consumableMapper.selectList(queryWrapper);
        return Result.ok(asmePersonInfoList);
    }

    @Override
    public Result<IPage<Consumable>> getListByConditionsWithPage(Consumable conditions) {
        IPage<Consumable> ipage = new Page<>(conditions.getCurrentPage(), conditions.getPageSize());
        LambdaQueryWrapper<Consumable> queryWrapper = new LambdaQueryWrapper<>();

        consumableMapper.selectPage(ipage, queryWrapper);
        setCreatorAndUpdater(ipage);

        return Result.ok(ipage);
    }


    @Override
    public Result<Consumable> insertOne(Consumable entity) {


        int inserted = consumableMapper.insert(entity);

        if (inserted > 0) {

            setCreatorAndUpdater(entity);

            return Result.ok(entity).msg("新增无损检测人员证书成功。");
        } else {
            return Result.error(entity).msg("新增无损检测人员证书失败。");
        }

    }

    @Override
    public Result<Consumable> updateOne(Consumable entity) {


        setVersion(entity);
        int updateById = consumableMapper.updateById(entity);
        if (updateById > 0) {

            setCreatorAndUpdater(entity);

            return Result.ok(entity).msg("修改无损检测人员证书成功。");
        } else {
            return Result.error(entity).msg("修改无损检测人员证书失败。");
        }
    }


}
